package com.jokey.vue.configuate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jokey.vue.model.RespBean;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2021/11/26 18:15
 * @version: v1.0.0
 * 权限认证处理器
 */
@Component
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
	
	@Override
	public void handle(HttpServletRequest request,
					   HttpServletResponse response,
					   AccessDeniedException e) throws IOException, ServletException {
		response.setStatus(HttpServletResponse.SC_FORBIDDEN);
		response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
		PrintWriter writer = response.getWriter();
		RespBean error = RespBean.error("权限不足，请联系管理员！");
		writer.write(new ObjectMapper().writeValueAsString(error));
		writer.flush();
		writer.close();
	}
}
